package com.zjq.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zjq.pojo.Address;
import com.zjq.pojo.Cart;
import com.zjq.pojo.Order;
import com.zjq.pojo.OrderList;
import com.zjq.service.BookStoreService;
import com.zjq.service.OrderListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Controller
@RequestMapping("/orderList")
public class OrderListController {

    @Autowired
    @Qualifier("OrderListServiceImpl")
    private OrderListService orderListService;

    @Autowired
    @Qualifier("BookStoreServiceImpl")
    private BookStoreService bookStoreService;

    @RequestMapping("/addOrderList")
    public String addOrderList(Order order,String city,String county,String detailed,int userId,String num,String name,String remarks,int price){
        String format = new SimpleDateFormat("yy-MM-dd HH:mm:ss").format(new Date());
        int orderNum;
        while (true){
            orderNum = havaOrder();
            Order o = orderListService.queryOrderById(orderNum);
            if (o == null){
                break;
            }
        }
        System.out.println("+++++++++++++++++++++++--------------------"+orderNum);
        Address address = new Address(orderNum,city,county,detailed,userId,num,name);
        orderListService.addAddress(address);
        System.out.println("=======================================");
        if (remarks == null){
            Order o1 = new Order(orderNum, format, userId, orderNum, price, 1);
            orderListService.addOrder(o1);
        }else {
            Order o1 = new Order(orderNum, format, userId, orderNum, remarks, 1, price);
            orderListService.addOrder(o1);
        }
        for (int j = 0; j < order.getOrderLists().size();j++){
            OrderList orderList = order.getOrderLists().get(j);
            orderList.setOrderNum(orderNum);
            System.out.println("+++++++++++++++++++++++--------------------"+orderList);
            orderListService.addOrderList(orderList);
        }
        List<Cart> carts = bookStoreService.queryCartByUserId(userId);
        for (Cart cart : carts){
            bookStoreService.deletCartById(cart.getCartId());
        }
        System.out.println("----------------------++++++++++++++++"+order.getOrderLists().toString());
        return "index";
    }

    public int havaOrder(){
        StringBuffer sb = new StringBuffer();
        for (int i = 0;i < 9;i++){
            sb.append(new Random().nextInt(10));
        }
        int orderNum = Integer.valueOf(String.valueOf(sb));
        return orderNum;
    }

    @RequestMapping("queryAllOrder")
    public String queryAllOrder(@RequestParam(value = "pn", required = true, defaultValue = "1") Integer pn, HttpServletRequest request, Model model){
        PageHelper.startPage(pn,5);
        List<Order> orders = orderListService.queryAllOrder();
        System.out.println("-------------"+orders);
        PageInfo<Order> order = new PageInfo<Order>(orders);
        model.addAttribute("order",order);
        return "backstage/book/order_list";
    }


    @RequestMapping("orderDetail")
    public String queryOrderListDetailById(int id,Model model){
        Order order = orderListService.queryOrderListDetailById(id);
        model.addAttribute("order",order);
        return "backstage/book/order_detail";
    }
}
